package com.yc.dynamic.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源配置
 */
@Configuration
public class DataSourceConfig {

    @Bean("testDataSource")
    @ConfigurationProperties("spring.datasource.test")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Bean("test1DataSource")
    @ConfigurationProperties("spring.datasource.test1")
    public DataSource test1DataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Autowired
    @Qualifier("dynamicDataSource")
    private DataSource testDataSource;

    @Resource
    private JpaProperties jpaProperties;

    @Autowired
    private EntityManagerFactoryBuilder entityManagerFactoryBuilder;


    @Bean(name = "dynamicDataSource")
    @DependsOn({ "testDataSource", "test1DataSource"})
    @Primary
    public DynamicDataSource dynamicDataSource(){
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map<Object, Object> targetDataSources=new HashMap<>(16);
        targetDataSources.put(DSName.test1DataSource,test1DataSource());
        targetDataSources.put(DSName.testDataSource,testDataSource());
        dynamicDataSource.setTargetDataSources(targetDataSources);
        dynamicDataSource.setDefaultTargetDataSource(testDataSource());
        return  dynamicDataSource;
    }

    @Primary
    @Bean(name = "entityManagerFactoryTest")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryTest() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = entityManagerFactoryBuilder
                .dataSource(testDataSource)
                //设置实体类所在位置
                .packages("com.yc.test.bean")
                .persistenceUnit("testPersistenceUnit")
                .properties(jpaProperties.getProperties())
                .build();
        return entityManagerFactory;
    }

    @Primary
    @Bean(name = "entityManagerTest")
    public EntityManager entityManagerTest() {
        return entityManagerFactoryTest().getObject().createEntityManager();
    }

    @Primary
    @Bean(name = "transactionManagerTest")
    public PlatformTransactionManager transactionManagerTest() {
        return new JpaTransactionManager(entityManagerFactoryTest().getObject());
    }
}
